import {DateStoreVO, TimeStampStore} from "@interface/index";
import {Injectable} from "@angular/core";

/**
 * @description 获取时间戳 默认是 0 点 0 分 0 秒
 * @author @ouxh-862
 * @date 2018-2-24
 * @params year, month, day, hours, min, sec, Milliseconds
 *  如果不传则取今天
 * @returns {number}
 */
export function getDateTimeStamp(y?, M?, d?, h = 0, m = 0, s = 0, ms = 0): number {
  // 如果不传就取今天
  if (!y && !M && !d) {
    const temp = new Date();
    temp.setHours(h);
    temp.setMinutes(m);
    temp.setSeconds(s);
    temp.setMilliseconds(ms);
    return temp.getTime();
  }
  return new Date(y, M, d, h, m, s, ms).getTime();
}

/**
 * @description 获取日期
 * @author @ouxh-862
 * @date 2018-2-24
 * @params year, month, day
 * @returns {number}
 */
export function getDateNum(year, month, day): number {
  return new Date(year, month, day).getDate();
}

/**
 * @description 日期类
 * @author @ouxh-862
 * @date 2018-2-24
 * @property getDate: DateStoreVO; // 各种日期的数字
 * @property getTimeStampStore: TimeStampStore; // 各种日期的时间戳
 * @注意 此类所储存的周计算里 一周的 第一天为星期一
 */
@Injectable()
export class DateStoreService {
  private dateStore: DateStoreVO; // 日期
  private timeStampStore: TimeStampStore;  // 时间戳
  constructor() { }

  get getTimeStampStore(): TimeStampStore {
    this.update();
    return this.timeStampStore;
  }

  get getDate(): DateStoreVO {
    this.updateDate();
    return this.dateStore;
  }
  /**
   * @description 更新储存的所有时间对象
   * @author @ouxh-862
   * @date 2018-2-24
   */
  update() {
    this.updateDate();
    this.updateTimeStemp();
  }

  /**
   * @description 更新时间 DateStoreVO
   * @author @ouxh-862
   * @date 2018-2-24
   */
  updateDate() {
    const date = new Date();
    const dateStore: DateStoreVO = {};
    // 年月日星期
    dateStore.today = date.getDate();
    dateStore.month = date.getMonth();
    dateStore.year = date.getFullYear();
    dateStore.weekDay = date.getDay();
    dateStore.yesterday = getDateNum(dateStore.year, dateStore.month, dateStore.today - 1);  // 昨天
    // 上月、本月最候一天
    dateStore.lastMonthLastDate = getDateNum(dateStore.year, dateStore.month, 0);  // 上个月最后一天
    dateStore.MonthLastDate = getDateNum(dateStore.year, dateStore.month + 1, 0);  // 这个月最后一天
    // 当前计算周的第一天为 星期一
    if (dateStore.weekDay === 0) { // 先判断今天是不是周天
      dateStore.weekStart = getDateNum(dateStore.year, dateStore.month, dateStore.today - 6);
      dateStore.weekEnd = getDateNum(dateStore.year, dateStore.month, dateStore.today);
      dateStore.LastWeekStart =  getDateNum(dateStore.year, dateStore.month, dateStore.today - 13);  // 上周第一天
      dateStore.LastWeekEnd =  getDateNum(dateStore.year, dateStore.month, dateStore.today - 7);  // 上周最后一天
    }else {  // 如果今天不是周天
      dateStore.weekStart = getDateNum(dateStore.year, dateStore.month, dateStore.today - dateStore.weekDay + 1);
      dateStore.weekEnd = getDateNum(dateStore.year, dateStore.month, dateStore.today + 7 - dateStore.weekDay);
      dateStore.LastWeekStart = getDateNum(dateStore.year, dateStore.month, dateStore.today - dateStore.weekDay + 1 - 7);  // 上周第一天
      dateStore.LastWeekEnd = getDateNum(dateStore.year, dateStore.month, dateStore.today  - dateStore.weekDay );  // 上周最后一天
    }
    this.dateStore = dateStore;
  }

  /**
   * @description 更新时间戳 TimeStampStore
   * @author @ouxh-862
   * @date 2018-2-24
   */
  updateTimeStemp() {
    const timeStempStore: TimeStampStore = {};
    const dateStore = this.dateStore;
    // 今天、昨天
    timeStempStore.today = getDateTimeStamp();
    timeStempStore.yesterday = getDateTimeStamp(dateStore.year, dateStore.month, dateStore.today - 1);  // 昨天
    // 本月、上月的第一天和最后一天
    timeStempStore.lastMonthEndDate = getDateTimeStamp(dateStore.year, dateStore.month, 0); // 上个月最后一天
    timeStempStore.lastMonthStartDate = getDateTimeStamp(dateStore.year, dateStore.month - 1, 1); // 上个月最后一天
    timeStempStore.monthEndDate = getDateTimeStamp(dateStore.year, dateStore.month + 1, 0);  // 这个月最后一天
    timeStempStore.monthStartDate = getDateTimeStamp(dateStore.year, dateStore.month, 1);  // 这个月第一天
    // 今年去年的第一天和最后一天
    timeStempStore.yearStartDate = getDateTimeStamp(dateStore.year, 0, 1);  // 今年第一天
    timeStempStore.lastYearStartDate = getDateTimeStamp(dateStore.year - 1, 0, 1);  // 去年第一天
    timeStempStore.yearEndDate = getDateTimeStamp(dateStore.year, 11, 31);  // 今年最后一天
    timeStempStore.lastYearEndDate = getDateTimeStamp(dateStore.year - 1, 11, 31);  // 今年最后一天
    // 当前计算周的第一天为 星期一
    if (dateStore.weekDay === 0) { // 先判断今天是不是周天
      timeStempStore.weekStart = getDateTimeStamp(dateStore.year, dateStore.month, dateStore.today - 6);
      timeStempStore.weekEnd = getDateTimeStamp(dateStore.year, dateStore.month, dateStore.today);
      timeStempStore.LastWeekStart =  getDateTimeStamp(dateStore.year, dateStore.month, dateStore.today - 13);  // 上周第一天
      timeStempStore.LastWeekEnd =  getDateTimeStamp(dateStore.year, dateStore.month, dateStore.today - 7);  // 上周最后一天
    }else {  // 如果今天不是周天
      timeStempStore.weekStart = getDateTimeStamp(dateStore.year, dateStore.month, dateStore.today - dateStore.weekDay + 1);
      timeStempStore.weekEnd = getDateTimeStamp(dateStore.year, dateStore.month, dateStore.today + 7 - dateStore.weekDay);
      timeStempStore.LastWeekStart =
        getDateTimeStamp(dateStore.year, dateStore.month, dateStore.today - dateStore.weekDay + 1 - 7); // 上周第一天

      timeStempStore.LastWeekEnd = getDateTimeStamp(dateStore.year, dateStore.month, dateStore.today  - dateStore.weekDay );  // 上周最后一天
    }
    this.timeStampStore = timeStempStore;
  }

}
